package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PartyProbationary;
import com.ruoyi.system.service.IPartyProbationaryService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预备党员申请Controller
 * 
 * @author ruoyi
 * @date 2025-10-16
 */
@RestController
@RequestMapping("/system/probationary")
public class PartyProbationaryController extends BaseController
{
    @Autowired
    private IPartyProbationaryService partyProbationaryService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询预备党员申请列表
     */
    //("@ss.hasPermi('system:probationary:list')")
    @GetMapping("/list")
    public TableDataInfo list(PartyProbationary partyProbationary)
    {
        startPage();
        List<PartyProbationary> list = partyProbationaryService.selectPartyProbationaryList(partyProbationary);
        return getDataTable(list);
    }

    /**
     * 导出预备党员申请列表
     */
    //("@ss.hasPermi('system:probationary:export')")
    @Log(title = "预备党员申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartyProbationary partyProbationary)
    {
        List<PartyProbationary> list = partyProbationaryService.selectPartyProbationaryList(partyProbationary);
        ExcelUtil<PartyProbationary> util = new ExcelUtil<PartyProbationary>(PartyProbationary.class);
        util.exportExcel(response, list, "预备党员申请数据");
    }

    /**
     * 获取预备党员申请详细信息
     */
    //("@ss.hasPermi('system:probationary:query')")
    @GetMapping(value = "/{probationaryId}")
    public AjaxResult getInfo(@PathVariable("probationaryId") Long probationaryId)
    {
        return success(partyProbationaryService.selectPartyProbationaryByProbationaryId(probationaryId));
    }

    /**
     * 新增预备党员申请
     */
    //("@ss.hasPermi('system:probationary:add')")
    @Log(title = "预备党员申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PartyProbationary partyProbationary)
    {
        return toAjax(partyProbationaryService.insertPartyProbationary(partyProbationary));
    }

    /**
     * 修改预备党员申请
     */
    //("@ss.hasPermi('system:probationary:edit')")
    @Log(title = "预备党员申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PartyProbationary partyProbationary)
    {
        return toAjax(partyProbationaryService.updatePartyProbationary(partyProbationary));
    }

    /**
     * 审核预备党员申请
     */
    //("@ss.hasPermi('system:probationary:edit')")
    @Log(title = "预备党员申请审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody PartyProbationary partyProbationary)
    {
        partyProbationary.setAuditTime(new Date());
        String auditStatus = partyProbationary.getAuditStatus();
        PartyProbationary que = partyProbationaryService.selectPartyProbationaryByProbationaryId(partyProbationary.getProbationaryId());
        // 如果审核通过，更新用户的当前阶段为预备党员
        if ("1".equals(auditStatus)) {
            sysUserService.updateUserCurrentStage(que.getUserId(), "3");
        }

        return toAjax(partyProbationaryService.updatePartyProbationary(partyProbationary));
    }

    /**
     * 删除预备党员申请
     */
    //("@ss.hasPermi('system:probationary:remove')")
    @Log(title = "预备党员申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{probationaryIds}")
    public AjaxResult remove(@PathVariable Long[] probationaryIds)
    {
        return toAjax(partyProbationaryService.deletePartyProbationaryByProbationaryIds(probationaryIds));
    }
}
